package com.jjs.jcenterdemo.adapterhelper;

import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jjs.jcenterdemo.R;
import com.white.commonlib.adapter.recycle.GridStaggerDivider;

import java.util.List;

/**
 * Created by ytf on 2018/3/24 024.
 * Description:
 */

public class QquListAdapter extends BaseMultiItemQuickAdapter<ListBean, BaseViewHolder>
{

    public QquListAdapter(List<ListBean> data)
    {
        super(data);
        addItemType(0, R.layout.item_qqu_list_9g);
        addItemType(1, R.layout.item_qqu_list);
    }

    @Override
    protected void convert(BaseViewHolder helper, ListBean item)
    {
        switch(helper.getItemViewType())
        {
            case 0:
                helper.setText(R.id.title, item.title);
                RecyclerView inner = helper.getView(R.id.recycle);
                inner.setNestedScrollingEnabled(false);
                inner.setLayoutManager(new GridLayoutManager(mContext, 3, OrientationHelper.VERTICAL, false)
                {
                    @Override
                    public boolean canScrollVertically()
                    {
                        return false;
                    }

                    @Override
                    public boolean canScrollHorizontally()
                    {
                        return false;
                    }
                });
                final List<ListBean.SingleImgBean> ls = item.imgs;
                InnerAdapter ad = new InnerAdapter(R.layout.item_qqu_list_9g_img, ls);
                ad.setOnItemClickListener(new OnItemClickListener()
                {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position)
                    {
                        Toast.makeText(mContext, ls.get(position).img, Toast.LENGTH_SHORT).show();
                    }
                });
                inner.setAdapter(ad);
                break;
            case 1:
                ImageView cover = helper.getView(R.id.cover);
                ImageView head = helper.getView(R.id.user_head);
                Glide.with(mContext)
                        .load(item.img)
                        .into(cover);
                Glide.with(mContext)
                        .load(item.img)
                        .into(head);
                break;
        }
    }

    private class InnerAdapter extends BaseQuickAdapter<ListBean.SingleImgBean, BaseViewHolder>
    {

        public InnerAdapter(int layoutResId, @Nullable List<ListBean.SingleImgBean> data)
        {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ListBean.SingleImgBean item)
        {
            ImageView img = helper.getView(R.id.img);
            Glide.with(mContext)
                    .load(item.img)
                    .into(img);
        }

        @Override
        public int getItemCount()
        {
            int si = super.getItemCount();
            if(si > 9)
                si = 9;
            return si;
        }
    }
}
